//
//  SetViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/12/15.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "SetViewController.h"
#import "SetViewMode.h"
#import "UserProtocolViewController.h"

#define kSetInfoCell @"SetInfoCell"

@implementation SetViewController {
    NSMutableArray *_sections;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self registCell];
    self.tableView.canReload = YES;
    self.title = @"设置";
}

#pragma mark 注册cell

- (void) registCell {
    [self.tableView registerClass:[SetInfoCell class] forCellReuseIdentifier:kSetInfoCell];
}

#pragma -mark ------------------重新布局----------------------

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0 ,Main_Screen_Width, Main_Screen_Height - 64);
}

#pragma - mark ------------------自定义相关的东西----------------------

- (NSArray *)customSections {
    if (_sections == nil) {
        _sections = [NSMutableArray array];
        
        //头部的相关设置
        MCUTableCustomSection *sec = [MCUTableCustomSection createWithTarget:self];
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.cellForRowAtIndexSelector = @selector(cellViewForSetheadCell:);
        sec.numberOfRowsSelector = @selector(cellNumberForSetheadCell:);
        sec.heightForCellSelector = @selector(heightCellForSetheadCell:);
        sec.cellSelectedSelector = @selector(selectedForSetheadCell:);
        [_sections addObject:self.splitSection];
        [_sections addObject:sec];
        
        //底部的cell
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.numberOfRowsSelector = @selector(numberOfRowForSetBottomCell:);
        sec.cellForRowAtIndexSelector = @selector(cellViewForSetBottomCell:);
        sec.heightForCellSelector = @selector(heightCellForSetBottomCell:);
        sec.cellSelectedSelector = @selector(selectCellForSetBottomCell:);
        [_sections addObject:self.splitSection];
        [_sections addObject:sec];
    }
    return _sections;
}

#pragma -mark ------------------相关的代理方法----------------------

- (NSNumber *)heightCellForSetheadCell:(NSIndexPath *)indexPath {
    return  @50;
}
- (NSNumber *)cellNumberForSetheadCell:(NSIndexPath *)indexPath{
    return @4;
}

- (void)selectedForSetheadCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"section --- %ld",indexPath.section);
    NSLog(@"row ---- %ld",indexPath.row);
    
    if (indexPath.row == 1) {
        UserProtocolViewController *userVC = [[UserProtocolViewController alloc] init];
        [self.navigationController pushViewController:userVC animated:YES];
    }
    if (indexPath.row == 3) {
        [self clearCache];
        SetInfoCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.celarLab.text = @"0.00MB";
    }
}

- (UITableViewCell *)cellViewForSetheadCell:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
        cell.titleLab.text = @"账号管理";
        return cell;
    }
    else if (indexPath.row == 1) {
        SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
        cell.titleLab.text = @"用户协议";
        return cell;
    }
    else if (indexPath.row == 2) {
        SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
        cell.titleLab.text = @"意见反馈";
        return cell;
    }
    else if (indexPath.row == 3) {
        SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
        cell.titleLab.text = @"清理缓存";
        cell.celarLab.hidden = NO;
        cell.rightImageView.hidden = YES;
        cell.celarLab.text = [NSString stringWithFormat:@"%.2fMB",[self readCacheSize]];
        return cell;
    }
    else{
        SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
        cell.titleLab.text = @"关于我们";
        return cell;
    }
    
}

- (NSNumber *)heightCellForSetBottomCell:(NSIndexPath *)indexPath {
    return  @50;
}

- (NSNumber *)numberOfRowForSetBottomCell:(NSIndexPath *)indexPath{
    return @1;
}

- (void)selectCellForSetBottomCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self outAccount];
}

- (UITableViewCell *)cellViewForSetBottomCell:(NSIndexPath *)indexPath {
    SetInfoCell *cell = [[SetInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSetInfoCell];
    cell.titleLab.text = @"退出账号";
    return cell;
}


#pragma -mark ------------------缓存相关----------------------

//清理缓存
- (void)clearCache {
    NSString *cachesPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *subFilePaths = [fileManager subpathsAtPath:cachesPath];
    for (NSString *subFilePath in subFilePaths) {
        NSString *path = [cachesPath stringByAppendingPathComponent:subFilePath];
        [fileManager removeItemAtPath:path error:nil];
    }
}

- (CGFloat)readCacheSize {
    NSUInteger size = [self getCacheDate];
    return  size / 1024.0 / 1024.0 / 100;
}

- (NSUInteger)getCacheDate {
    NSUInteger size = 0;
    //找到缓存路径
    NSString *cache = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    
    //文件枚举，获取当前路径下的所有文件的属性
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:cache];
    //拿到文件夹所有的文件
    for (NSString *fileName in fileEnumerator) {
        //获取所有文件的路径
        NSString *filePath = [cache stringByAppendingPathComponent:fileName];
        //获取所有文件的属性
        NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL];
        //计算每个文件的大小，然后相加
        size += [dic fileSize];
    }
    return size;
}

- (void)outAccount {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"确定退出账号？" preferredStyle:UIAlertControllerStyleAlert];
    FFWeakSelf(self);
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertControllerStyleAlert handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"777");
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}

@end
